const mongoose = require('mongoose');
var Merchant = mongoose.model("merchant")                                  ;
var logoImage = require('./merchant.logo.save')
const request = require('request')
var config = require('../../../core/server/config/config')
var jmsPublish = require('../../../jms/jmsPublisher.queue')

exports.updateInfo = function (req,res) {

    var merchant = new Merchant(req.body)
    if(req.files.file){
        var fileName = req.files.file.path;
        var name = fileName.substring(fileName.lastIndexOf("\\") + 1,fileName.length);
        merchant.logo = name;
    }

    Merchant.findOneAndUpdate({_id:merchant._id}, merchant, {upsert:true}, function(err, m){
        if(!err){
            //console.log(JSON.stringify(merchant))
            delete merchant.password
            if(req.files.file){
                logoImage.saveImage(name,req.files.file.path)
            }else{
                merchant.logo = m.logo
            }

            req.session.merchant = merchant
            res.json({merchant:merchant,msg:'SUCCESS'})
            console.log('update merchant success')
        }else{
            res.json({msg:'ERROR'})
            console.log('update merchant faild')
        }
    });
};

exports.login = function(req, res){
    var mname = req.body.mname
    var password = req.body.password;
    Merchant.findOne({mname:mname},function (err,merchant) {
        if(err){
            res.send(500)
            console.log(err)
        }else if(!merchant){
            req.session.error = 'mname is not found';
            res.json({msg:'mname is not found'})
        }else{
            if(password != merchant.password){
                req.session.error = 'password is wrong!'
                res.json({msg:'password is wrong'})
            }else{
                delete merchant.password
                req.session.merchant = merchant

                console.log(merchant._id.toString())
                console.log()
                request(config.checkStatus + merchant._id.toString(), function (error, response, data) {
                    if (!error && response.statusCode == 200) {
                        merchant.status = Number(data);
                        console.log(data)
                    } else if(error){
                        console.error('request Merchant Server error : ' + error);
                    } else {
                        console.error('request Merchant Server status error');
                    }
                })
                res.json({merchant:merchant,msg:'SUCCESS'})
            }
        }
    })
};

exports.checkmName = function (req, res) {
    var mname = req.body.mname;
    console.log(mname);
    Merchant.findOne({mname:mname},function (err,merchant) {
        if(err){
            console.log(err)
        }else if(!merchant){
            console.log(merchant + "////");
            res.json({msg:false})
        }else if(merchant){
            res.json({msg:true})
        }
    })
};

exports.regist = function (req, res) {
    var merchant = new Merchant(req.body)
    var m = req.body;
    var fileName = req.files.file.path;
    var name = fileName.substring(fileName.lastIndexOf("\\") + 1,fileName.length);
    console.log(name);

    merchant.logo = name;
    merchant.grade = 3;
    merchant.save(function (err,m) {
        if(!err){
            console.log('save merchant success')
            logoImage.saveImage(name,req.files.file.path)
            jmsPublish.publicMerchant(m._id.toString())
            res.json({msg:'SUCCESS'})
        }else{
            console.log({msg:'ERROR'})
            res.json({msg:'ERROR',data:'mname is exists'})
        }
    })
};

exports.logout = function (req,res) {
    //console.log(req.session.merchant.mname)
    req.session.destroy();
    res.json({msg:'SUCCESS'})
}

exports.loadMerchant = function (req,res) {
    var merchant = req.session.merchant;
    if(merchant){
        request(config.checkStatus + merchant._id.toString(), function (error, response, data) {
            if (!error && response.statusCode == 200) {
                merchant.status = Number(data);
                req.session.merchant = merchant
                res.json(merchant)
                console.log(data)
            } else if(error){
                req.session.merchant = merchant
                console.error('request Merchant Server error : ' + error);
                res.json(merchant)
            } else {
                console.error('request Merchant Server status error');
            }
        })
    }else{

        res.json(merchant)
    }

}

